<?php
    class Contenu extends Admin_Controller{
        function Contenu(){
            parent::Admin_Controller();
            $this->load->model('Contenu_m');
        }
        function index(){
            is_admin();
            $this->list_contents();
        }
        function list_contents(){
            is_admin();
            $per_page = $this->uri->segment(3);
            if($per_page==''){
                $per_page=7;
            }
            $off_set = $this->uri->segment(4);
            if($off_set==''){
                $off_set=0;
            }
            $config['base_url'] = base_url().'index.php/contenu/list_contents/'.$per_page;
            $config['uri_segment'] = 4;
            $config['per_page'] = '7';
            $config['total_rows'] = $this->db->count_all('content');
            if($config['total_rows']==$off_set&&$off_set>0){
                $off_set=$off_set-$per_page;
            }
            $this->pagination->initialize($config);
            $this->_data['query'] = $this->Contenu_m->get_all_contents($per_page,$off_set);
            $this->_data['pagination']=$this->pagination->create_links();
            $this->_data['per_page'] = $per_page;
            $this->_data['off_set'] = $off_set;
            $this->_load_view('contenu/list_contents');
        }
        function delete_content($id,$per_page,$off_set){
            is_admin();
            $this->Contenu_m->delete_content($id);
            redirect('contenu/list_contents/'.$per_page.'/'.$off_set);
        }
        function add_content(){
            is_admin();
            $config = array(
            array(
            'field'   => 'firstnameu',
            'label'   => 'First name',
            'rules'   => 'required'
            ),
            array(
            'field'   => 'lastname',
            'label'   => 'Last name',
            'rules'   => 'required'
            ),
            array(
            'field'   => 'address',
            'label'   => 'Address',
            'rules'   => 'required'
            ),
            array(
            'field'   => 'txtDes',
            'label'   => 'Description',
            'rules'   => 'required'
            )
            );
            $this->form_validation->set_rules($config);
            if($this->input->post('submit')){
                $birthday=   $this->input->post('birth_year').'-'.$this->input->post('birth_month').'-'.$this->input->post('birth_date');
                $leave_day = $this->input->post('leave_year').'-'.$this->input->post('leave_month').'-'.$this->input->post('leave_day');
                $date1 = strtotime($birthday);
                $date2 = strtotime($leave_day);
                //echo $date1.'-----'.$date2;die;
                if($date1<$date2){
                    if($this->form_validation->run()){
                        $image_path = './assets/news_img';
                        $thumb_path = './assets/thumbs';
                        $config = array(
                        'allowed_types' => "jpg|jpeg|gif|png",
                        'upload_path' => $image_path,
                        'max_size'=>2000
                        );
                        $this->load->library('upload', $config);
                        if (!$this->upload->do_upload())
                        {
                            $this->_data['error'] = $this->upload->display_errors();
                            $this->_load_view('contenu/add_content');
                        }    
                        else
                        {
                            $upload_info = $this->upload->data();
                            $config = array(
                            'source_image' => $upload_info['full_path'], //get original image
                            'new_image' => $thumb_path, //save as new image //need to create thumbs first
                            'maintain_ratio' => true,
                            'width' => 300,
                            'height' => 200
                            );
                            $this->load->library('image_lib', $config); //load library
                            $this->image_lib->resize(); //do whatever specified in config
                            $data = array(
                            'name'=>$this->input->post('firstnameu'),
                            'prename'=>$this->input->post('lastname'),
                            'birthday'=>$birthday,
                            'death'=>$leave_day,
                            'date_post'=>mdate("%Y-%m-%d",time()),
                            'address'=>$this->input->post('address'),
                            'description'=>$this->input->post('txtDes'),
                            'image'=>$upload_info['file_name'],
                            'user_id'=>$this->session->userdata('adminid'),
                            'is_paid'=>1 
                            );
                            $this->Contenu_m->post_content($data);
                            redirect(site_url('contenu/list_contents'));
                        }

                    } else{
                        $this->_load_view('contenu/add_content');
                    }
                } else{
                    $this->_data['error'] = 'invalid date selected';
                    $this->_load_view('contenu/add_content');
                }

            } else{
                $this->_load_view('contenu/add_content');
            }
        }
        function edit_content($id){
            is_admin();
            $config = array(
            array(
            'field'   => 'firstnameu',
            'label'   => 'First name',
            'rules'   => 'required'
            ),
            array(
            'field'   => 'lastname',
            'label'   => 'Last name',
            'rules'   => 'required'
            ),
            array(
            'field'   => 'address',
            'label'   => 'Address',
            'rules'   => 'required'
            ),
            array(
            'field'   => 'txtDes',
            'label'   => 'Description',
            'rules'   => 'required'
            )
            );
            $this->form_validation->set_rules($config);
            $query=$this->Contenu_m->get_content_by_id($id);
            $this->_data['query']=$query;
            if($query->num_rows<1){
                redirect('avis_admin/list_news');
            }
            if($this->input->post('submit')){
                is_admin();                  
                $birthday=   $this->input->post('birth_year').'-'.$this->input->post('birth_month').'-'.$this->input->post('birth_date');
                $leave_day = $this->input->post('leave_year').'-'.$this->input->post('leave_month').'-'.$this->input->post('leave_day');
                $date1 = strtotime($birthday);
                $date2 = strtotime($leave_day);
                if($date1<$date2){
                    if($this->form_validation->run()){
                        $image_path = './assets/news_img';
                        $thumb_path = './assets/thumbs';
                        $config = array(
                        'allowed_types' => "jpg|jpeg|gif|png",
                        'upload_path' => $image_path,
                        'max_size'=>4000
                        );
                        $this->load->library('upload', $config);

                        $birthday=   $this->input->post('birth_year').'-'.$this->input->post('birth_month').'-'.$this->input->post('birth_day');
                        $leave_day = $this->input->post('leave_year').'-'.$this->input->post('leave_month').'-'.$this->input->post('leave_day');
                        if (!$this->upload->do_upload())
                        {
                            $data = array(
                            'name'=>$this->input->post('firstnameu'),
                            'prename'=>$this->input->post('lastname'),
                            'birthday'=>$birthday,
                            'death'=>$leave_day,
                            'date_post'=>time(),
                            'address'=>$this->input->post('address'),
                            'description'=>$this->input->post('txtDes'),
                            'user_id'=>$this->session->userdata('admin') 
                            );
                            $this->Contenu_m->update_content($data,$id);
                            redirect(site_url('contenu/list_contents'));
                        }    
                        else
                        {
                            $info = $query->row();
                            $image_to_delete = './assets/news_img/'.$info->image;
                            $thumb_to_delete = './assets/thumbs/'.$info->image;
                            //print_r($image_to_delete);die;
                            delete_image($image_to_delete,$thumb_to_delete);
                            $upload_info = $this->upload->data();
                            $config = array(
                            'source_image' => $upload_info['full_path'], //get original image
                            'new_image' => $thumb_path, //save as new image //need to create thumbs first
                            'maintain_ratio' => true,
                            'width' => 300,
                            'height' => 200
                            );
                            $this->load->library('image_lib', $config); //load library
                            $this->image_lib->resize(); //do whatever specified in config 
                            $data = array(
                            'name'=>$this->input->post('firstnameu'),
                            'prename'=>$this->input->post('lastname'),
                            'birthday'=>$birthday,
                            'death'=>$leave_day,
                            'date_post'=>mdate("%Y-%m-%d",time()),
                            'address'=>$this->input->post('address'),  
                            'description'=>$this->input->post('txtDes'),
                            'image'=>$upload_info['file_name'],
                            'user_id'=>$this->session->userdata('admin') 
                            );
                            $this->Contenu_m->update_content($data,$id);
                            redirect(site_url('contenu/list_contents'));
                        }

                    } else{
                        $this->_load_view('contenu/edit_content');
                    }
                } else{
                    $this->_data['error'] = 'invalid date selected';
                    $this->_load_view('contenu/edit_content');
                }
            } else{
                $this->_load_view('contenu/edit_content');
            }
        }
        function activate_news($id,$per_page,$off_set){
            $this->Contenu_m->activate_new($id);
            redirect('contenu/list_contents/'.$per_page.'/'.$off_set);
        }
        function deactivate_news($id,$per_page,$off_set){
            $this->Contenu_m->deactivate_new($id);
            redirect('contenu/list_contents/'.$per_page.'/'.$off_set);
        }
    }
?>
